﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace HP.UtilsLib
{
    public static class FileHelper
    {
        public static async Task<IRandomAccessStream> ToRandomAccessStreamAsync(string filePath)
        {
            try
            {
                var path = Path.GetDirectoryName(filePath);
                var filename = Path.GetFileName(filePath);
                var folder = await StorageFolder.GetFolderFromPathAsync(path);
                var file = await folder.GetFileAsync(filename);
                return await file.OpenAsync(FileAccessMode.Read);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static async Task<string> ToStringAsync(StorageFolder folder, string filename)
        {
            try
            {
                var file = await folder.GetFileAsync(filename);
                var stream = await file.OpenAsync(FileAccessMode.Read);
                using (var inputstream = stream.GetInputStreamAt(0))
                using (var dataReader = new DataReader(inputstream))
                {
                    uint numBytesLoaded = await dataReader.LoadAsync((uint)stream.Size);
                    return dataReader.ReadString(numBytesLoaded);
                }
            }
            catch(Exception)
            {
                return null;
            }
        }

        public static async Task<byte[]> ToBytesAsync(StorageFolder folder, string filename)
        {
            try
            {
                var file = await folder.GetFileAsync(filename);
                var stream = await file.OpenAsync(FileAccessMode.Read);
                using (var inputstream = stream.GetInputStreamAt(0))
                using (var reader = new DataReader(inputstream))
                {
                    reader.ByteOrder = ByteOrder.LittleEndian;
                    uint numBytesLoaded = await reader.LoadAsync((uint)stream.Size);
                    Debug.WriteLine($"numBytesLoaded:{numBytesLoaded}");
                    byte[] bytes = new byte[numBytesLoaded];
                    reader.ReadBytes(bytes);

                    return bytes;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static async Task WriteBytesAsync(StorageFile file, byte[] data)
        {
            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var outputStream = stream.GetOutputStreamAt(0);
                var writer = new DataWriter(outputStream)
                {
                    ByteOrder = ByteOrder.LittleEndian,
                    UnicodeEncoding = UnicodeEncoding.Utf8
                };
                writer.WriteBytes(data);
                await writer.StoreAsync();
                await outputStream.FlushAsync();
            }
        }
    }
}
